import axios, {AxiosResponse} from 'axios';
import qs from 'querystring';
import {FetchData} from '../types/api';

export const resolveData = (aResponse: AxiosResponse) => {
  return aResponse.data;
};
export const getAxiosInstance = () => {
  const ins = axios.create({
    baseURL: 'https://www.codebest.top/api/',
    timeout: 10 * 1000,
  });

  ins.interceptors.response.use(res => {
    return res.data;
  });

  return ins;
};

export const getApi = (url = '', params = {}) => {
  return getAxiosInstance().get(url + `?${qs.stringify(params)}`) as Promise<
    FetchData<any>
  >;
};
